package service

import (
	"context"
	"fmt"
	__ "hospital-srv/basic/proto"
	"hospital-srv/handler/dao"
)

// 用户手动添加健康数据
func (s *Serve) UserHealthDataCreated(_ context.Context, in *__.UserHealthDataCreatedReq) (*__.UserHealthDataCreatedResp, error) {
	if in.UserId <= 0 {
		return nil, fmt.Errorf("参数错误")
	}

	if in.BodyTemperature < 36.1 || in.BodyTemperature > 37.2 {
		return nil, fmt.Errorf("体温数据异常或尽快就医")
	}

	if in.BloodSugar >= 11.1 {
		return nil, fmt.Errorf("血糖数据异常或尽快就医")
	}

	if in.BloodPressure < 90 || in.BloodPressure > 140 {
		return nil, fmt.Errorf("高压数据异常或尽快就医")
	}

	if in.BloodPressureD < 60 || in.BloodPressureD > 90 {
		return nil, fmt.Errorf("低压数据异常或尽快就医")
	}

	if in.BloodOxygen < 95 || in.BloodOxygen > 99 {
		return nil, fmt.Errorf("血氧数据异常或尽快就医")
	}

	created, err := dao.UserHealthDataCreated(in)
	if err != nil {
		return nil, fmt.Errorf("用户手动添加健康数据失败")
	}

	return &__.UserHealthDataCreatedResp{
		Id: uint64(created.Id),
	}, nil
}

// 获取用户健康数据
func (s *Serve) GetUserHealthData(_ context.Context, in *__.GetUserHealthDataReq) (*__.GetUserHealthDataResp, error) {
	if in.UserId <= 0 || in.Page <= 0 || in.PageSize <= 0 {
		return nil, fmt.Errorf("参数错误")
	}

	data, err := dao.GetUserHealthData(in)
	if err != nil {
		return nil, fmt.Errorf("用户获取健康信息失败")
	}

	return &__.GetUserHealthDataResp{
		List: data,
	}, nil
}
